//
//  StatusViewModel.swift
//  JGWB
//
//  Created by 付建国 on 2016/10/13.
//  Copyright © 2016年 sher. All rights reserved.
//

import UIKit

class StatusViewModel: NSObject {
    
    // MARK:- 定义属性
    var status : Status?
    var cellHeight : CGFloat = 0
    
    // MARK:- 对数据处理后的属性
    var sourceText : String?
    var createAtText : String?
    
    // MARK:- 用户数据处理
    var verifiedImage : UIImage?
    var vipImage : UIImage?
    var profileURL : URL?
    var picURLs : [URL] = [URL]()
    
    // MARK:- 自定义构造函数
    init(status : Status) {
        self.status = status
        
        //处理来源数据
        if let source = status.source, status.source != ""{
            //对来源属性处理
            //获取起始位置和截取的长度
            let startIndex = (source as NSString).range(of: ">").location + 1
            let length = (source as NSString).range(of: "</").location - startIndex
            
            sourceText = (source as NSString).substring(with: NSRange.init(location: startIndex, length: length))
        }
       
        //处理时间
        if let createAt = status.created_at{
            createAtText = Date.createDateStr(createAtStr: createAt)
        }
        
        //处理认证方式
        let verfiedType = status.user?.verified_type ?? -1
        switch verfiedType {
        case 0:
            verifiedImage = UIImage.init(named: "avatar_vip")
        case 2, 3, 5:
            verifiedImage = UIImage.init(named: "avatar_enterprise_vip")
        case 220:
            verifiedImage = UIImage.init(named: "avatar_grassroot")
        default:
            verifiedImage = nil
        }
        
        //处理会员等级
        let mbrank = status.user?.mbrank ?? 0
        if mbrank > 0 && mbrank <= 6{
            vipImage = UIImage.init(named: "common_icon_membership_level\(mbrank)")
        }
        
        //处理头像的URL
        let profileURLString = status.user?.profile_image_url ?? ""
        profileURL = URL.init(string: profileURLString)
        
        //处理配图的URL
        let picURLDicts = status.pic_urls!.count != 0 ? status.pic_urls : status.retweeted_status?.pic_urls
        
        if let picURLDicts = picURLDicts{
            for picURLDict in picURLDicts {
                guard let picURLString = picURLDict["thumbnail_pic"] else{
                    continue
                }
                
                picURLs.append(URL.init(string: picURLString)!)
            }
        }
    }

}
















